
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>ANTLR 使用 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="visitor.html" />
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../log.html">
            
                <a href="../log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">零：前置准备</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    配环境、跑测试
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../lab0/env.html">
            
                <a href="../lab0/env.html">
            
                    
                    环境配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../lab0/testing.html">
            
                <a href="../lab0/testing.html">
            
                    
                    运行测试样例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../lab0/riscv.html">
            
                <a href="../lab0/riscv.html">
            
                    
                    RISC-V 的工具链使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">第一个编译器</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    step1：仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    从零开始的 lexer、parser 以及汇编生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    词法语法分析工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    使用中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1.4.1" data-path="antlr.html">
            
                <a href="antlr.html">
            
                    
                    ANTLR 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4.2" data-path="visitor.html">
            
                <a href="visitor.html">
            
                    
                    Visitor 模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4.3" data-path="ir.html">
            
                <a href="ir.html">
            
                    
                    IR 简明介绍
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="spec.html">
            
                <a href="spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">常量表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <span>
            
                    
                    step2：一元操作
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../lab2/intro.html">
            
                <a href="../lab2/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../lab2/guide.html">
            
                <a href="../lab2/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../lab2/spec.html">
            
                <a href="../lab2/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" >
            
                <span>
            
                    
                    step3：加减乘除模
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../lab3/intro.html">
            
                <a href="../lab3/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../lab3/guide.html">
            
                <a href="../lab3/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.3" data-path="../lab3/spec.html">
            
                <a href="../lab3/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" >
            
                <span>
            
                    
                    step4：比较和逻辑表达式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../lab4/intro.html">
            
                <a href="../lab4/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../lab4/guide.html">
            
                <a href="../lab4/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.3" data-path="../lab4/spec.html">
            
                <a href="../lab4/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">变量和语句</li>
        
        
    
        <li class="chapter " data-level="5.1" >
            
                <span>
            
                    
                    step5：局部变量和赋值
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../lab5/intro.html">
            
                <a href="../lab5/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../lab5/guide.html">
            
                <a href="../lab5/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.3.1" data-path="../lab5/stackframe.html">
            
                <a href="../lab5/stackframe.html">
            
                    
                    栈帧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.1.4" data-path="../lab5/spec.html">
            
                <a href="../lab5/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    step6：if 语句和条件表达式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.2.1" data-path="../lab6/intro.html">
            
                <a href="../lab6/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.2" data-path="../lab6/guide.html">
            
                <a href="../lab6/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.3" data-path="../lab6/spec.html">
            
                <a href="../lab6/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">块语句和作用域和更多语句</li>
        
        
    
        <li class="chapter " data-level="6.1" >
            
                <span>
            
                    
                    step7：作用域和块语句
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../lab7/intro.html">
            
                <a href="../lab7/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../lab7/guide.html">
            
                <a href="../lab7/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../lab7/spec.html">
            
                <a href="../lab7/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" >
            
                <span>
            
                    
                    step8：循环语句
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../lab8/intro.html">
            
                <a href="../lab8/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../lab8/guide.html">
            
                <a href="../lab8/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="../lab8/spec.html">
            
                <a href="../lab8/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">函数和全局变量</li>
        
        
    
        <li class="chapter " data-level="7.1" >
            
                <span>
            
                    
                    step9：函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../lab9/intro.html">
            
                <a href="../lab9/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../lab9/guide.html">
            
                <a href="../lab9/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.3.1" data-path="../lab9/calling.html">
            
                <a href="../lab9/calling.html">
            
                    
                    函数调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../lab9/spec.html">
            
                <a href="../lab9/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" >
            
                <span>
            
                    
                    step10：全局变量
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../lab10/part0-intro.html">
            
                <a href="../lab10/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../lab10/part1-parser.html">
            
                <a href="../lab10/part1-parser.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../lab10/part1-1-task.html">
            
                <a href="../lab10/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../lab10/part4-codegen.html">
            
                <a href="../lab10/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.5" data-path="../lab10/part4-1-task.html">
            
                <a href="../lab10/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.6" data-path="../lab10/summary.html">
            
                <a href="../lab10/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.7" data-path="../lab10/spec.html">
            
                <a href="../lab10/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">指针和数组</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    step11：指针
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../lab11/part0-intro.html">
            
                <a href="../lab11/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../lab11/typeck.html">
            
                <a href="../lab11/typeck.html">
            
                    
                    类型检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../lab11/part4-codegen.html">
            
                <a href="../lab11/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.4" data-path="../lab11/part4-1-task.html">
            
                <a href="../lab11/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.5" data-path="../lab11/summary.html">
            
                <a href="../lab11/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.6" data-path="../lab11/spec.html">
            
                <a href="../lab11/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8.2" >
            
                <span>
            
                    
                    step12：数组
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.2.1" data-path="../lab12/part0-intro.html">
            
                <a href="../lab12/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.2" data-path="../lab12/part4-codegen.html">
            
                <a href="../lab12/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.3" data-path="../lab12/part4-1-task.html">
            
                <a href="../lab12/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.4" data-path="../lab12/summary.html">
            
                <a href="../lab12/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.5" data-path="../lab12/spec.html">
            
                <a href="../lab12/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">参考资料</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../REFERENCE.html">
            
                <a href="../../REFERENCE.html">
            
                    
                    参考资料
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >ANTLR 使用</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="antlr-&#x4F7F;&#x7528;&#x2014;&#x2014;&#x4EE5;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;&#x4E3A;&#x4F8B;">ANTLR &#x4F7F;&#x7528;&#x2014;&#x2014;&#x4EE5;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;&#x4E3A;&#x4F8B;</h1>
<p>&#x4F7F;&#x7528; ANTLR &#x5DE5;&#x5177;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x5199;&#x51FA;&#x8BCD;&#x6CD5;&#x548C;&#x8BED;&#x6CD5;&#x5206;&#x6790;&#x7684; <strong>&#x89C4;&#x8303;&#xFF08;specification&#xFF09;</strong>&#xFF0C;
&#x7136;&#x540E;&#x5B83;&#x4F1A;&#x5E2E;&#x6211;&#x4EEC;&#x751F;&#x6210; lexer &#x548C; parser &#x4E43;&#x81F3; visitor&#xFF0C;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x7528;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5; <sup><a href="#fn_1" id="reffn_1">1</a></sup> &#x6765;&#x4ECB;&#x7ECD; ANTLR&#xFF0C;&#x8868;&#x8FBE;&#x5F0F;&#x7531;&#x4E00;&#x7CFB;&#x5217;&#x6574;&#x6570;&#x901A;&#x8FC7;&#x52A0;&#x51CF;&#x4E58;&#x9664;&#x4EE5;&#x53CA;&#x62EC;&#x53F7;&#x6784;&#x6210;&#xFF0C;&#x4F8B;&#x5982; <code>(1+3)*4-3-3</code>&#x3002;</p>
<p>&#x5BF9;&#x4E8E; ANTLR&#xFF0C;&#x8BCD;&#x6CD5;&#x548C;&#x8BED;&#x6CD5;&#x5206;&#x6790;&#x7684;&#x89C4;&#x8303;&#x90FD;&#x5199;&#x5728; .g4 <sup><a href="#fn_2" id="reffn_2">2</a></sup> &#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x4F8B;&#x5982;&#x6211;&#x4EEC;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x89C4;&#x8303;&#x662F;<a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/ExprLex.g4" target="_blank">&#x6587;&#x6CD5;: ExprLex.g4</a>&#x548C;<a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/Expr.g4" target="_blank">&#x8BED;&#x6CD5;: Expr.g4</a>&#x3002;
&#x65E0;&#x8BBA;&#x662F;&#x8BCD;&#x6CD5;&#x89C4;&#x8303;&#x8FD8;&#x662F;&#x8BED;&#x6CD5;&#x89C4;&#x8303;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x89C4;&#x8303;&#x6587;&#x4EF6;&#x7ED3;&#x6784;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x5982;&#x4E0B;&#x3002;</p>
<blockquote>
<p>&#x89C4;&#x8303;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;<code>//</code> &#x8868;&#x793A;&#x6CE8;&#x91CA;&#xFF0C;&#x89C4;&#x8303;&#x662F;&#x5927;&#x5C0F;&#x5199;&#x654F;&#x611F;&#x7684;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x7528;&#x5355;&#x5F15;&#x53F7;&#x62EC;&#x8D77;&#x3002;</p>
</blockquote>
<ol>
<li><p>&#x5F00;&#x5934;&#x58F0;&#x660E; <strong>&#x89C4;&#x8303;&#x540D;</strong>&#xFF0C;&#x9700;&#x8981;&#x548C;&#x6587;&#x4EF6;&#x540D;&#x4E00;&#x81F4;&#xFF1A;</p>
<pre><code class="lang-antlr4"> // [ExprLex.g4] &#x8BCD;&#x6CD5;&#x89C4;&#x8303;&#xFF0C;&#x7528; lexer grammar &#x6807;&#x8BC6;&#xFF0C;&#x884C;&#x5C3E;&#x6709;&#x5206;&#x53F7;&#x3002;
 lexer grammar ExprLex;

 // [Expr.g4] &#x8BED;&#x6CD5;&#x89C4;&#x8303;&#xFF0C;&#x7528; grammar &#x6807;&#x8BC6;&#xFF0C;&#x884C;&#x5C3E;&#x6709;&#x5206;&#x53F7;&#x3002;
 grammar Expr;
</code></pre>
</li>
<li><p>&#x7136;&#x540E;&#x53EF;&#x80FD;&#x6709;&#x4E00;&#x4E9B; <strong>&#x89C4;&#x8303;&#x81EA;&#x8EAB;&#x7684;&#x8BBE;&#x7F6E;</strong>&#xFF0C;&#x89C1;&#x540E;&#x9762; &#x201C;&#x8BED;&#x6CD5;&#x89C4;&#x8303;&#x201D;</p>
</li>
<li><p>&#x7136;&#x540E;&#x662F; <strong>&#x4E00;&#x7CFB;&#x5217;&#x89C4;&#x5219;</strong>&#xFF0C;&#x89C4;&#x5219;&#x7C7B;&#x4F3C;&#x4E0A;&#x4E0B;&#x6587;&#x65E0;&#x5173;&#x8BED;&#x6CD5;&#x7684;&#x4EA7;&#x751F;&#x5F0F;&#x3002;
&#x6BCF;&#x6761;&#x89C4;&#x5219;&#x5305;&#x542B;&#x5DE6;&#x53F3;&#x4E24;&#x8FB9;&#xFF0C;&#x7528;&#x5192;&#x53F7;&#x9694;&#x5F00;&#xFF0C; <em>&#x5DE6;&#x8FB9;</em> &#x662F;&#x4E00;&#x4E2A;&#x7B26;&#x53F7;&#xFF0C;&#x53EF;&#x4EE5;&#x7531; <em>&#x53F3;&#x8FB9;</em> &#x89C4;&#x7EA6;&#x800C;&#x6765;&#x3002;
&#x7B26;&#x53F7;&#x5206;&#x4E3A; <strong>&#x7EC8;&#x7ED3;&#x7B26;</strong> &#x548C; <strong>&#x975E;&#x7EC8;&#x7ED3;&#x7B26;</strong> &#xFF0C;&#x7EC8;&#x7ED3;&#x7B26;&#x7528;&#x5927;&#x5199;&#x5B57;&#x6BCD;&#x6253;&#x5934;&#xFF0C;&#x975E;&#x7EC8;&#x7ED3;&#x7B26;&#x7528;&#x5C0F;&#x5199;&#x5B57;&#x6BCD;&#x3002;
&#x7C7B;&#x4F3C;&#x4EA7;&#x751F;&#x5F0F;&#xFF0C;&#x5982;&#x679C;&#x591A;&#x6761;&#x89C4;&#x5219;&#x7684;&#x5DE6;&#x8FB9;&#x76F8;&#x540C;&#xFF0C;&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x5408;&#x5E76;&#x5199;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x53F3;&#x624B;&#x8FB9;&#x7528;&#x7AD6;&#x7EBF;&#x9694;&#x5F00;&#x3002;</p>
<pre><code class="lang-antlr4"> // [ExprLex.g4] &#x8BCD;&#x6CD5;&#x89C4;&#x5219;&#xFF0C;&#x89C4;&#x5219;&#x672B;&#x5C3E;&#x6709;&#x5206;&#x53F7;&#x3002;
 Integer: [0-9]+;

 // [Expr.g4] &#x8BED;&#x6CD5;&#x89C4;&#x5219;&#xFF0C;&#x89C4;&#x5219;&#x672B;&#x5C3E;&#x6709;&#x5206;&#x53F7;
 atom
     : &apos;(&apos; expr &apos;)&apos;  // &#x4E00;&#x4E2A;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x89C4;&#x7EA6;&#x5230; atom
     | Integer       // &#x6574;&#x6570; token &#x53EF;&#x4EE5;&#x89C4;&#x7EA6;&#x5230; atom
     ;
</code></pre>
</li>
</ol>
<h2 id="&#x8BCD;&#x6CD5;&#x89C4;&#x8303;">&#x8BCD;&#x6CD5;&#x89C4;&#x8303;</h2>
<p><a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/ExprLex.g4" target="_blank">&#x8BCD;&#x6CD5;&#x89C4;&#x8303;</a>&#x63CF;&#x8FF0;&#x4E86; lexer &#x5E94;&#x8BE5;&#x600E;&#x4E48;&#x751F;&#x6210;&#xFF0C;&#x663E;&#x7136;&#x8BCD;&#x6CD5;&#x89C4;&#x8303;&#x4E2D;&#x89C4;&#x5219;&#x7684;&#x5DE6;&#x8FB9;&#x53EA;&#x80FD;&#x662F;&#x7EC8;&#x7ED3;&#x7B26;&#x3002;
&#x9664;&#x4E86;&#x4E0A;&#x9762;&#x6240;&#x8BF4;&#x7684;&#xFF0C;&#x8BCD;&#x6CD5;&#x89C4;&#x8303;&#x8FD8;&#x6709;&#x4E00;&#x70B9;&#x662F;&#xFF1A;&#x89C4;&#x5219;&#x7684;&#x53F3;&#x624B;&#x8FB9;&#x662F;&#x4E00;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>&#x8BE6;&#x7EC6;&#x7528;&#x6CD5;&#x5728;<a href="https://github.com/antlr/antlr4/blob/master/doc/lexer-rules.md" target="_blank">&#x8FD9;&#x91CC;</a>&#xFF0C;&#x4E00;&#x4E9B;&#x5E38;&#x89C1;&#x7528;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-antlr4">// 1. &#x4E3A;&#x4E86;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#xFF0C;&#x7528;&#x5355;&#x5F15;&#x53F7;&#x628A;&#x5B83;&#x62EC;&#x8D77;&#x6765;
Lparen: &apos;(&apos;;

// 2. [0-9] &#x5339;&#x914D; (char)&apos;0&apos; &#x5230; (char)&apos;9&apos; &#x4E4B;&#x95F4;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x7C7B;&#x4F3C;&#x5176;&#x4ED6; regex &#x7684; \d &#x6216;&#x8005; [[:digit:]]
// 3. &#x52A0;&#x53F7; + &#x8868;&#x793A;&#x5B83;&#x524D;&#x9762;&#x7684;&#x7247;&#x6BB5;&#x53EF;&#x4EE5;&#x5339;&#x914D;&#x4E00;&#x6B21;&#x6216;&#x591A;&#x6B21;&#xFF0C;&#x7C7B;&#x4F3C;&#x6709; * &#x7684;&#x96F6;&#x6B21;&#x6216;&#x591A;&#x6B21;&#xFF0C;? &#x7684;&#x96F6;&#x6B21;&#x6216;&#x4E00;&#x6B21;&#x3002;
//    &#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x8D2A;&#x5A6A;&#x7684;&#xFF0C;&#x4F1A;&#x5339;&#x914D;&#x5C3D;&#x91CF;&#x591A;&#x7684;&#x6B21;&#x6570;&#x3002;&#x548C;&#x5176;&#x4ED6; regex &#x4E00;&#x6837;&#xFF0C;&#x7247;&#x6BB5;&#x53EF;&#x4EE5;&#x7528; ( ) &#x5206;&#x7EC4;&#x3002;
Integer: [0-9]+;

// 4. fragment &#x8868;&#x793A; WhitespaceChar &#x672C;&#x8EAB;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x7B26;&#x53F7;&#xFF0C;&#x5B83;&#x53EA;&#x662F;&#x4E00;&#x4E2A; regex &#x7684;&#x7247;&#x6BB5;&#xFF0C;lexer &#x4E0D;&#x4F1A;&#x4EA7;&#x751F;&#x5B83;&#x7684; token&#x3002;
//    &#x5B83;&#x548C; minilexer &#x4E2D;&#x7684; whitespaceChar &#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;
// 5. [ \t\n\r] &#x5339;&#x914D;&#x4E00;&#x4E2A;&#x7A7A;&#x683C; (ascii &#x7801; 0x20)&#xFF0C;&#x6216;&#x8005;&#x4E00;&#x4E2A;&#x5236;&#x8868;&#x7B26; (0x9)&#xFF0C;&#x6216;&#x8005;&#x4E00;&#x4E2A;&#x6362;&#x884C;&#x7B26; (0xa) &#x6216;&#x8005;&#x4E00;&#x4E2A;&#x56DE;&#x8F66; (0xd)
fragment WhitespaceChar: [ \t\n\r];
// 6. Whitespace &#x5339;&#x914D;&#x8F93;&#x5165;&#x4E2D;&#x7684;&#x7A7A;&#x767D;&#x3002;&#x7C7B;&#x4F3C; minilexer&#xFF0C;&quot;-&gt; skip&quot; &#x8868;&#x793A;&#x5FFD;&#x7565;&#x6B64;&#x7EC8;&#x7ED3;&#x7B26;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5339;&#x914D;&#x4EE5;&#x540E;&#x4E0D;&#x4EA7;&#x751F;&#x5BF9;&#x5E94;&#x7684; token&#x3002;
Whitespace: WhitespaceChar+ -&gt; skip;
</code></pre>
<h2 id="&#x8BED;&#x6CD5;&#x89C4;&#x8303;">&#x8BED;&#x6CD5;&#x89C4;&#x8303;</h2>
<p><a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/Expr.g4" target="_blank">&#x8BED;&#x6CD5;&#x89C4;&#x8303;</a>&#x63CF;&#x8FF0;&#x4E86; parser &#x5E94;&#x8BE5;&#x600E;&#x4E48;&#x751F;&#x6210;&#x3002;&#x9664;&#x4E86;&#x4E0A;&#x9762;&#x8BF4;&#x7684;&#xFF0C;&#x8FD8;&#x9700;&#x6CE8;&#x610F;&#xFF1A;</p>
<ol>
<li><p>parser &#x4F9D;&#x8D56;&#x4E8E; lexer&#xFF0C;&#x6240;&#x4EE5;&#x8BED;&#x6CD5;&#x89C4;&#x8303;&#x4E2D;&#x9700;&#x8981; <strong>&#x5BFC;&#x5165;&#x8BCD;&#x6CD5;&#x89C4;&#x8303;</strong></p>
<pre><code class="lang-antlr4"> // &#x5BFC;&#x5165;&#x8BCD;&#x6CD5;&#x89C4;&#x8303;
 import ExprLex;
</code></pre>
<blockquote>
<p>&#x5176;&#x5B9E; ANTLR &#x4E0D;&#x8981;&#x6C42;&#x4F60;&#x5206;&#x5F00; lexer &#x548C; parser&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x628A; import &#x8BED;&#x53E5;&#x6362;&#x6210; ExprLex &#x91CC;&#x9762;&#x7684;&#x6240;&#x6709;&#x89C4;&#x5219;&#xFF0C;
&#x6548;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>
<p>&#x4F46;&#x5206;&#x5F00; lexer &#x548C; parser &#x66F4;&#x5E72;&#x51C0;&#xFF0C;&#x5E76;&#x4E14;&#x4E5F;&#x65B9;&#x4FBF; lexer &#x590D;&#x7528;&#x3002;
&#x5404;&#x79CD;&#x8BED;&#x8A00;&#x867D;&#x7136;&#x8BED;&#x6CD5;&#x5DEE;&#x522B;&#x5F88;&#x5927;&#xFF0C;&#x8BCD;&#x6CD5;&#xFF08;&#x7A7A;&#x767D;&#x3001;&#x6574;&#x6570;&#x3001;&#x6807;&#x8BC6;&#x7B26;&#x3001;&#x6807;&#x70B9;&#x7B26;&#x53F7;&#x7B49;&#xFF09;&#x5374;&#x6CA1;&#x592A;&#x5927;&#x5DEE;&#x522B;&#x3002;</p>
</blockquote>
</li>
<li><p>parser &#x89C4;&#x5219;&#x7684;&#x53F3;&#x624B;&#x8FB9;&#x9664;&#x4E86;&#x7B26;&#x53F7;&#x4EE5;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x6709; <strong>&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;</strong>&#x3002;
 &#x5982;&#x679C;&#x5B83;&#x80FD;&#x88AB;&#x89C4;&#x7EA6;&#x5230;&#x8BCD;&#x6CD5;&#x89C4;&#x8303;&#x91CC;&#x67D0;&#x4E2A;&#x7B26;&#x53F7;&#xFF0C;&#x90A3;&#x5B83;&#x5C31;&#x7B49;&#x4EF7;&#x4E8E;&#x90A3;&#x4E2A;&#x7B26;&#x53F7;&#xFF1B;
 &#x5426;&#x5219; ANTLR &#x5185;&#x90E8;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x4E34;&#x65F6;&#x7EC8;&#x7ED3;&#x7B26; <code>T__xxx</code>&#xFF0C;&#x5B83;&#x7684;&#x89C4;&#x5219;&#x7684;&#x53F3;&#x8FB9;&#x662F;&#x90A3;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x3002;</p>
<pre><code class="lang-antlr4"> mulOp : &apos;*&apos; | &apos;/&apos; ; // &#x7B49;&#x4EF7;&#x4E8E; mulOp : Mul | Div ;
</code></pre>
</li>
<li><p>&#x4F60;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x7ED9; <strong>&#x89C4;&#x5219;&#x547D;&#x540D;</strong>&#x3002;
 &#x5728;&#x751F;&#x6210;&#x7684; AST &#x91CC;&#xFF0C;atom &#x5BF9;&#x5E94;&#x7684;&#x7ED3;&#x70B9;&#x4F1A;&#x88AB;&#x5206;&#x4E3A;&#x4E24;&#x7C7B;&#xFF1A;atomParen &#x548C; atomInteger&#xFF0C;
 &#x5B83;&#x4EEC;&#x62E5;&#x6709;&#x7684;&#x5B57;&#x6BB5;&#x4E0D;&#x540C;&#xFF0C;&#x4E5F;&#x5BF9;&#x5E94;&#x4E0D;&#x540C;&#x7684; visit &#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-antlr4"> atom
     : &apos;(&apos; expr &apos;)&apos;      # atomParen
     | Integer           # atomInteger
     ;
</code></pre>
</li>
<li><p>&#x89C4;&#x5219;&#x5176;&#x5B9E;&#x662F;&#x7528; <strong>EBNF (extended Barkus-Naur form)</strong> &#x8BB0;&#x53F7;&#x4E66;&#x5199;&#x7684;&#xFF0C;EBNF &#x4E5F;&#x662F;&#x63CF;&#x8FF0;&#x4E0A;&#x4E0B;&#x6587;&#x65E0;&#x5173;&#x8BED;&#x6CD5;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3002;
 &#x76F8;&#x5BF9;&#x666E;&#x901A;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x65E0;&#x5173;&#x8BED;&#x6CD5;&#x8BB0;&#x53F7;&#xFF0C;EBNF &#x5141;&#x8BB8;&#x4F60;&#x5728;&#x89C4;&#x5219;&#x5185;&#x90E8;&#x4F7F;&#x7528; <code>|</code> &#x63CF;&#x8FF0;&#x9009;&#x62E9;&#x3001;<code>*</code> &#x6216; <code>?</code> &#x6216; <code>+</code> &#x63CF;&#x8FF0;&#x91CD;&#x590D;&#xFF0C;<code>(</code>&#x548C;<code>)</code> &#x5206;&#x7EC4; <sup><a href="#fn_3" id="reffn_3">3</a></sup>&#x3002;
 &#x4F8B;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x7528;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-antlr4"> add
     // 1. &#x4F7F;&#x7528;&#x62EC;&#x53F7;&#x5206;&#x7EC4;&#xFF0C;&#x5206;&#x7EC4;&#x5185;&#x90E8;&#x4F7F;&#x7528; | &#x63CF;&#x8FF0;&#x9009;&#x62E9;
     // 2. &#x548C; EBNF &#x65E0;&#x5173;&#xFF0C;&#x4F46; op &#x662F;&#x7ED9;&#x8FD9;&#x4E2A;&#x7B26;&#x53F7;&#x7684;&#x547D;&#x540D;&#xFF0C;&#x7136;&#x540E; add &#x7684; AST &#x7ED3;&#x70B9;&#x4F1A;&#x6709;&#x4E00;&#x4E2A; op &#x5B57;&#x6BB5;&#x3002;
     : add op=(Add|Sub) mul
     | mul
     ;

 mul
     // 3. &#x4F7F;&#x7528; * &#x63CF;&#x8FF0;&#x96F6;&#x6B21;&#x6216;&#x591A;&#x6B21;&#x7684;&#x91CD;&#x590D;&#x3002;+ &#x548C; ? &#x7C7B;&#x4F3C;&#x3002;
     : atom (mul atom)*
     ;
</code></pre>
<p> &#x5173;&#x4E8E; EBNF&#xFF0C;&#x518D;&#x4E3E;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;&#x63CF;&#x8FF0;&#x96F6;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x7528;&#x9017;&#x53F7;&#x9694;&#x5F00;&#x7684; <code>expr</code> &#x5217;&#x8868;&#xFF0C;&#x4E0B;&#x9762;&#x4E24;&#x79CD;&#x5199;&#x6CD5;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF0C;&#x4F46; EBNF &#x8BB0;&#x53F7;&#x66F4;&#x7B80;&#x77ED;&#x3002;</p>
<pre><code class="lang-antlr4"> // &#x4F20;&#x7EDF;&#x5199;&#x6CD5;
 exprList
     :           # emptyExprList
     | exprList2 # nonemptyExprList
     ;
 exprList2
     : expr
     | expr &apos;,&apos; exprList2
     ;

 // EBNF &#x5199;&#x6CD5;
 exprList
     : (expr (&apos;,&apos; expr)*)?
     ;
</code></pre>
</li>
</ol>
<h2 id="&#x8FD0;&#x884C;-antlr">&#x8FD0;&#x884C; ANTLR</h2>
<p>&#x5B89;&#x88C5; ANTLR&#xFF0C;&#x8BBE;&#x7F6E; <code>CLASSPATH</code> &#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x914D;&#x7F6E; <code>antlr4</code> &#x548C; <code>grun</code> &#x7684; alias &#x540E;&#xFF0C;&#x8FD0;&#x884C;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4; <sup><a href="#fn_4" id="reffn_4">4</a></sup>&#xFF1A;</p>
<pre><code class="lang-bash">$ antlr4 Expr.g4                    <span class="hljs-comment"># &#x4F1A;&#x81EA;&#x52A8;&#x62C9;&#x53D6; import &#x7684; ExprLex.g4</span>
$ ls ExprLexer.java ExprParser.java <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x751F;&#x6210; Java &#x7684; lexer &#x548C; parser&#xFF0C;&#x5176;&#x4ED6;&#x6587;&#x4EF6;&#x4E0D;&#x7528;&#x7BA1;</span>
ExprLexer.java  ExprParser.java
$ javac *.java
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;(1+3)*4-3-3&quot;</span> &gt; input        <span class="hljs-comment"># &#x8F93;&#x5165;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x662F; (1+3)*4-3-3</span>
$ grun Expr expr -gui input         <span class="hljs-comment"># &#x8F93;&#x51FA;&#x5982;&#x4E0B;&#x56FE;</span>
</code></pre>
<p><img src="pics/grun.png" alt=""></p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5C1D;&#x8BD5;&#x628A;&#x6700;&#x540E;&#x4E00;&#x6B65;&#x7684; <code>-gui</code> &#x6362;&#x6210; <code>-tokens</code>&#x3001;<code>-tree</code> &#x770B;&#x770B;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x7ED9;&#x51FA;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF0C;&#x53D9;&#x8FF0;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x751F;&#x6210;&#x7684; lexer &#x548C; parser&#x3002;</p>
<ol>
<li><p><a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/Main.java" target="_blank">Main.java</a> &#x662F; Java &#x7684;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x3002;&#x505A;&#x5B8C;&#x4E0A;&#x9762;&#x6B65;&#x9AA4;&#x540E;&#xFF0C;&#x8FD0;&#x884C; Main&#xFF1A;</p>
<pre><code class="lang-bash">$ java Main &lt; input
(expr (add (add (add (mul (atom ( (expr (add (add (mul (atom 1))) + (mul (atom 3)))) )) (mulOp *) (atom 4))) - (mul (atom 3))) - (mul (atom 3))))
</code></pre>
</li>
<li><p><a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/main.py" target="_blank">main.py</a> &#x662F; Python &#x7684;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x3002;&#x4E3A;&#x4E86;&#x8FD0;&#x884C;&#x5B83;&#xFF0C;&#x9664;&#x4E86;&#x5B89;&#x88C5; ANTLR &#x4F60;&#x8FD8;&#x9700;&#x8981;&#x5B89;&#x88C5; Python &#x7684; ANTLR API&#xFF0C;&#x89C1;<a href="https://github.com/decaf-lang/minidecaf/blob/md-dzy/README.md#%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE" target="_blank">&#x8FD9;&#x91CC;</a>&#x3002;&#x8FD0;&#x884C;&#x65B9;&#x6CD5;&#x5982;&#x4E0B;</p>
<pre><code class="lang-bash">$ antlr4 Expr.g4 -Dlanguage=Python3
$ ls ExprParser.py ExprLexer.py <span class="hljs-comment"># &#x751F;&#x6210;&#x4E86; Python &#x7684; lexer &#x548C; parser</span>
ExprLexer.py  ExprParser.py
$ python3 main.py
(expr (add (add (add (mul (atom ( (expr (add (add (mul (atom 1))) + (mul (atom 3)))) )) (mulOp *) (atom 4))) - (mul (atom 3))) - (mul (atom 3))))
</code></pre>
</li>
</ol>
<h2 id="visitor-&#x7684;&#x4F7F;&#x7528;">Visitor &#x7684;&#x4F7F;&#x7528;</h2>
<blockquote>
<p>ANTLR &#x9ED8;&#x8BA4;&#x751F;&#x6210; listener&#xFF0C;&#x5B83;&#x5141;&#x8BB8;&#x4F60;&#x5728;&#x904D;&#x5386; AST &#x8FC7;&#x7A0B;&#x8FDB;&#x5165;&#x7ED3;&#x70B9;&#x548C;&#x79BB;&#x5F00;&#x7ED3;&#x70B9;&#x7684;&#x65F6;&#x5019;&#x8FD0;&#x884C;&#x4E00;&#x4E9B;&#x4EE3;&#x7801;&#xFF0C;&#x4F46;&#x6211;&#x4EEC;&#x4E0D;&#x7528; listener&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; visitor&#x3002;</p>
</blockquote>
<p>&#x9996;&#x5148;&#x7528;&#x53C2;&#x6570; <code>-visitor</code> &#x544A;&#x8BC9; ANTLR &#x751F;&#x6210; visitor &#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-bash">$ antlr4 Expr.g4 -visitor
</code></pre>
<p>visitor &#x4EE3;&#x7801;&#x5728; ExprVisitor.java &#x548C; ExprBaseVisitor.java &#x4E2D;&#x3002;
&#x524D;&#x8005;&#x5B9A;&#x4E49;&#x63A5;&#x53E3;&#xFF0C;&#x540E;&#x8005;&#x662F;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0;&#xFF1A;&#x53EA;&#x904D;&#x5386;&#x3001;&#x4E0D;&#x505A;&#x5176;&#x4ED6;&#x4E8B;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExprBaseVisitor</span>&lt;<span class="hljs-title">T</span>&gt; <span class="hljs-keyword">extends</span> <span class="hljs-title">AbstractParseTreeVisitor</span>&lt;<span class="hljs-title">T</span>&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title">ExprVisitor</span>&lt;<span class="hljs-title">T</span>&gt; </span>{
    <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> T <span class="hljs-title">visitExpr</span><span class="hljs-params">(ExprParser.ExprContext ctx)</span> </span>{ <span class="hljs-keyword">return</span> visitChildren(ctx); }
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;ANTLR &#x7684; visitor &#x548C;<a href="visitor.html">&#x6211;&#x4EEC;&#x7684;</a>&#x57FA;&#x672C;&#x4E00;&#x81F4;&#xFF1A;</p>
<ul>
<li>visit &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x662F; <code>T</code></li>
<li>&#x4ED6;&#x6240;&#x8C13; context &#x5C31;&#x662F; AST &#x7684;&#x7ED3;&#x70B9;&#xFF0C;&#x6BCF;&#x4E2A; context &#x4E5F;&#x6709;&#x4E00;&#x4E2A; accept &#x51FD;&#x6570;&#x63A5;&#x53D7; visitor</li>
<li>&#x4F46;&#x4ED6;&#x7684; visitor &#x8FD8;&#x81EA;&#x5E26;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5; visitChildren&#xFF1A;&#x904D;&#x5386;&#x6240;&#x6709;&#x5B50;&#x7ED3;&#x70B9;&#x3002;&#x8FD4;&#x56DE;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5B50;&#x7ED3;&#x70B9;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;</li>
</ul>
<p>ANTLR &#x751F;&#x6210;&#x7684; python visitor &#x4E5F;&#x5DEE;&#x4E0D;&#x591A;</p>
<pre><code class="lang-bash">$ antlr4 Expr.g4 -visitor -Dlanguage=Python3
</code></pre>
<p>visitor &#x5728; ExprVisitor.py &#x91CC;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># ExprVisitor.py</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExprVisitor</span><span class="hljs-params">(ParseTreeVisitor)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">visitExpr</span><span class="hljs-params">(self, ctx:ExprParser.ExprContext)</span>:</span>
        <span class="hljs-keyword">return</span> self.visitChildren(ctx)
    <span class="hljs-comment"># ...</span>
</code></pre>
<p><a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/MainEval.java" target="_blank">MainEval.java</a> &#x548C; <a href="https://github.com/decaf-lang/minidecaf-tutorial-code/blob/master/step1/maineval.py" target="_blank">maineval.py</a> &#x901A;&#x8FC7;&#x8868;&#x8FBE;&#x5F0F;&#x6C42;&#x503C;&#x5C55;&#x73B0;&#x4E86; visitor &#x7684;&#x7528;&#x6CD5;&#xFF0C;&#x5982;&#x4E0A;&#x7F16;&#x8BD1;&#x540E;&#x5982;&#x4E0B;&#x8FD0;&#x884C;&#x5373;&#x53EF;&#x3002;
&#x8F93;&#x51FA;&#x7684; 10 &#x5C31;&#x7B49;&#x4E8E; <code>(1+3)*4-3-3</code>&#x3002;</p>
<pre><code>$ python3 mainvisitor.py &lt; input
10

$ java MainVisitor &lt; input
10
</code></pre><h2 id="&#x5E38;&#x89C1;&#x95EE;&#x9898;">&#x5E38;&#x89C1;&#x95EE;&#x9898;</h2>
<ul>
<li><p>javac &#x62A5;&#x9519;&#x4E00;&#x5806; <code>cannot find symbol</code></p>
<ul>
<li>&#x6CA1;&#x6709;&#x8BBE;&#x7F6E; <code>CLASSPATH</code></li>
</ul>
</li>
<li><p>grun &#x62A5;&#x9519; <code>Can&apos;t load Expr as lexer or parser</code></p>
<ul>
<li>&#x4F60; <code>antlr4</code> &#x4EE5;&#x540E;&#x6CA1;&#x6709;&#x7F16;&#x8BD1; java &#x6587;&#x4EF6;</li>
</ul>
</li>
<li><p>&#x6211;&#x7684;&#x8F93;&#x5165;&#x662F; <code>1+2 (((</code> &#x5B83;&#x7ADF;&#x7136;&#x4E0D;&#x62A5;&#x9519;</p>
<ul>
<li>ANTLR &#x4E0D;&#x5F3A;&#x5236;&#x6D88;&#x8017;&#x6574;&#x4E2A;&#x8F93;&#x5165;&#xFF0C;&#x6240;&#x4EE5; parser &#x505A;&#x5B8C; 1+2 &#x5C31;&#x505C;&#x4E86;&#x3002;</li>
<li>&#x53EF;&#x4EE5;&#x628A; <code>expr: add;</code> &#x6539;&#x6210; <code>expr: add EOF;</code></li>
</ul>
</li>
<li><p>antlr4 &#x62A5;&#x9519; <code>error(31):  ANTLR cannot generate python3 code as of version 4.8</code></p>
<ul>
<li><code>-Dlanguage=Python3</code> &#x7684; P &#x8981;&#x5927;&#x5199;</li>
</ul>
</li>
</ul>
<h2 id="&#x5907;&#x6CE8;">&#x5907;&#x6CE8;</h2>
<blockquote id="fn_1">
<sup>1</sup>. step1 &#x7684; MiniDecaf &#x8BED;&#x6CD5;&#x592A;&#x7B80;&#x5355;&#xFF0C;&#x4E0D;&#x80FD;&#x4F53;&#x73B0;&#x5F88;&#x591A; ANTLR &#x7684;&#x7279;&#x6027;&#x3002;<a href="#reffn_1" title="Jump back to footnote [1] in the text."> &#x21A9;</a>
</blockquote>
<blockquote id="fn_2">
<sup>2</sup>. g &#x662F; grammar&#xFF0C;4 &#x662F; ANTLR &#x7684;&#x7248;&#x672C;&#x53F7; 4&#x3002;<a href="#reffn_2" title="Jump back to footnote [2] in the text."> &#x21A9;</a>
</blockquote>
<blockquote id="fn_3">
<sup>3</sup>. EBNF &#x672C;&#x8EAB;&#x53C8;&#x6709;&#x5F88;&#x591A;&#x8BB0;&#x53F7;&#xFF0C;&#x6709;&#x7684;&#x4F7F;&#x7528; <code>{ ... }</code> &#x8868;&#x793A;&#x91CD;&#x590D;&#x3002;&#x6211;&#x4EEC;&#x63CF;&#x8FF0;&#x7684;&#x662F; ANTLR &#x7684; EBNF &#x8BB0;&#x53F7;&#x3002;<a href="#reffn_3" title="Jump back to footnote [3] in the text."> &#x21A9;</a>
</blockquote>
<blockquote id="fn_4">
<sup>4</sup>. &#x547D;&#x4EE4;&#x4ECE; <a href="https://www.antlr.org/" target="_blank">https://www.antlr.org/</a> &#x4E2D; Samples &#x7684;&#x5185;&#x5BB9;&#x4FEE;&#x6539;&#x800C;&#x6765;<a href="#reffn_4" title="Jump back to footnote [4] in the text."> &#x21A9;</a>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="visitor.html" class="navigation navigation-next navigation-unique" aria-label="Next page: Visitor 模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"ANTLR 使用","level":"3.1.4.1","depth":3,"next":{"title":"Visitor 模式","level":"3.1.4.2","depth":3,"path":"docs/lab1/visitor.md","ref":"docs/lab1/visitor.md","articles":[]},"previous":{"title":"其他","level":"3.1.4","depth":2,"ref":"","articles":[{"title":"ANTLR 使用","level":"3.1.4.1","depth":3,"path":"docs/lab1/antlr.md","ref":"docs/lab1/antlr.md","articles":[]},{"title":"Visitor 模式","level":"3.1.4.2","depth":3,"path":"docs/lab1/visitor.md","ref":"docs/lab1/visitor.md","articles":[]},{"title":"IR 简明介绍","level":"3.1.4.3","depth":3,"path":"docs/lab1/ir.md","ref":"docs/lab1/ir.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab1/antlr.md","mtime":"2020-09-02T07:27:37.403Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-08T05:38:47.833Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

